﻿@model Invoicing.Model.Accounts.UserModel


<div class="GeneralEdit" id="fmUser">
    <ul>
        <li>
            <label for="txtUserName">用户名：</label>
            <input type="text" class="k-textbox" id="txtUserName" name="username" value="@Model.UserName" maxlength="50" pattern=".{2,}" required validationMessage="请输入用户名" @(Model.ID>0?"readonly='readonly'":"")  />
            <span class="k-invalid-msg" data-for="username"></span>
        </li>
        <li>
            <label for="txtPassword">密码：</label>
            <input type="password" class="k-textbox" id="txtPassword" name="password"  maxlength="20" pattern ="(?=^.{8,20}$)(?=(?:.*?\d))(?=.*[a-zA-Z])[0-9a-zA-Z!#$%*()_+^&]*$" @(Model.ID==0?"required":"" ) validationMessage ="8-20个，数字和字母混合" />
            <span class="k-invalid-msg" data-for="password"></span>
        </li>
        <li>
            <label for="txtPasswordRepeat">重复密码：</label>
            <input type="password" class="k-textbox" id="txtPasswordRepeat" name="passwordRepeat"  maxlength="20"  />
        </li>
        <li>
            <label for="txtRealName">姓名：</label>
            <input type="text" class="k-textbox" id="txtRealName" name="realname" value="@Model.RealName" maxlength="50" pattern=".{2,}" required validationMessage="请输入姓名"  />
        </li>
        <li>
            <label for="txtMobile">手机号码：</label>
            <input type="text" class="k-textbox" id="txtMobile" name="mobile" value="@Model.Mobile" maxlength="20" />
        </li>
        <li>
            <label for="ddlStatus">状态：</label>
            <select id="ddlStatus">
                <option value="1" @(Model.Status==1?"selected='selected'":"")>正常</option>
                <option value="2" @(Model.Status==2?"selected='selected'":"")>禁用</option>
            </select>
        </li>
        <li>
            <label for="ddlRole">角色</label>
            <select id="ddlRole" multiple="multiple" data-placeholder="选择角色...">
                @foreach (var role in (Invoicing.Model.Accounts.RoleCollection)ViewBag.Roles)
                {
                    <option value="@role.ID" @((Model.Roles!=null && Model.Roles.Any(p=>p.ID==role.ID))?"selected=\"selected\"":"") >@role.Description</option>    
                }
            </select>
        </li>
        <li class="editButtons">
            <button id="btnSaveUser" class="k-button"><span class="k-icon k-i-tick"></span><span>确认提交</span></button>
            &nbsp;&nbsp;
            <button class="k-button" onclick="CancelGeneralEdit();"><span class="k-icon k-i-cancel"></span><span>&nbsp;返&nbsp;回</span></button>
        </li>
    </ul>
</div>

<script class="init">
    $("#ddlStatus").kendoDropDownList();

    $("#ddlRole").kendoMultiSelect();
</script>

<script class="save_user">
    $(function () {
        var validator = $("#fmUser").kendoValidator().data("kendoValidator");
        $("#btnSaveUser").click(function (e) {
            if ($("#txtPassword").val() != $("#txtPasswordRepeat").val()) {
                wndGeneralInformation.content("两次密码输入必须一致").open();
                return false;
            }
            if (validator.validate()) {
                var values = {
                    userID: "@Model.ID",
                    realname: $("#txtRealName").val(),
                    mobile: $("#txtMobile").val(),
                    username: $("#txtUserName").val(),
                    password: $("#txtPassword").val(),
                    status: $("#ddlStatus").data("kendoDropDownList").value(),
                    roles: $("#ddlRole").data("kendoMultiSelect").value().join(',')
                };

                Whale.Remote.Operation({
                    url: "/Accounts/User/Save",
                    parameters: values,
                    success: function (result) {
                        wndGeneralEdit.close();
                        if ($("#grid_user").length > 0)
                            $("#grid_user").data("kendoGrid").dataSource.read();
                    },
                    error: function (msg) {
                        wndGeneralInformation.content(msg).open();
                    }
                });
            }
            return false;
        });
    });
</script>
